<template>
  <a-modal
      :title="title"
      :width="800"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
      wrapClassName="ant-modal-cust-warp"
      style="top:5%;height: 85%;overflow-y: hidden">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model" :rules="rules">
        <a-form-model-item label="账户名称" required prop="username">
          <a-input v-model="model.username" :disabled="formDisabled" placeholder="请输入账户名称"/>
        </a-form-model-item>
        <a-form-model-item label="用户姓名" required prop="name">
          <a-input v-model="model.name" :disabled="formDisabled" placeholder="请输入用户姓名"/>
        </a-form-model-item>
        <a-form-model-item label="用户头像" prop="avatar">
          <single-upload
              v-model="model.avatar"
              placeholder="请选择用户用户头像"
              accept=".jpg,.jpeg,.png,.gif,.webp"/>
        </a-form-model-item>
        <a-form-model-item label="用户性别" required prop="sex">
          <a-select v-model="model.sex" :disabled="formDisabled" placeholder="请选择用户性别">
            <a-select-option
                v-for="item in sexOptions"
                :key="item.id"
                :value="item.value"
                :label="item.name"
            >
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="出生日期" required prop="birthday">
          <a-date-picker
              style="width: 100%"
              v-model="model.birthday"
              :disabled="formDisabled"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
              placeholder="请选择出生日期"
          />
        </a-form-model-item>
        <a-form-model-item label="电话" required prop="phone">
          <a-input v-model="model.phone" :disabled="formDisabled" placeholder="请输入用户电话"/>
        </a-form-model-item>
        <a-form-model-item label="地址" required prop="address">
          <a-input v-model="model.address" :disabled="formDisabled" placeholder="请输入用户地址"/>
        </a-form-model-item>
        <a-form-model-item label="角色" required prop="roleIds">
          <a-select mode="multiple" v-model="model.roleIds" :disabled="formDisabled" placeholder="请选择角色">
            <a-select-option
                v-for="item in roleOptions"
                :key="item.id"
                :value="item.id"
                :label="item.name"
            >
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import SingleUpload from "@/components/upload/SingleUpload.vue";
import moment from "moment";
import {save} from '@/services/user'
import {list as roleList} from '@/services/role'
import {single} from '@/services/dict'

export default {
  name: "UserForm",
  components: {SingleUpload},
  data() {
    return {
      title: "操作",
      visible: false,
      formDisabled: false,
      isOkDisabled: false,
      model: {},
      roleOptions: [],
      sexOptions: [],
      layout: {
        labelCol: {span: 3},
        wrapperCol: {span: 20},
      },
      confirmLoading: false,
      rules: {
        username: [
          {required: true, message: '请输入用户名!'},
        ],
        name: [
          {required: true, message: '请输入姓名!'},
        ],
        sex: [
          {required: true, message: '请选择性别!'},
        ],
        roleIds: [
          {required: true, message: '请至少选择一个角色!'},
        ],
        address: [
          {required: true, message: '请输入用户地址!'},
        ]
      },
    }
  },
  mounted() {
    this.initDict()
  },
  methods: {
    moment,
    add() {
      this.title = "添加用户";
      this.model = {};
      this.formDisabled = false
      this.isOkDisabled = false
      this.visible = true;
    },
    edit(record) {
      this.title = "编辑用户";
      this.model = Object.assign({}, record);
      this.formDisabled = false
      this.isOkDisabled = false
      this.visible = true;
    },
    view(record) {
      this.title = "查看用户";
      this.model = Object.assign({}, record);
      this.formDisabled = true
      this.isOkDisabled = true
      this.visible = true;
    },
    submit() {
      if (this.isOkDisabled) {
        this.visible = false;
      } else {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.confirmLoading = true;
            console.log('$f(submit)=>', this.model);
            save(this.model).then(res => {
              if (res.data.code === 200) {
                this.$message.success('操作成功' || res.data.msg);
                this.$emit('refresh');
              } else {
                this.$message.error('操作失败' || res.data.msg);
              }
            }).catch(error => {
              this.$message.error('操作失败' || error.message);
            }).finally(() => {
              this.confirmLoading = false;
            })
            this.visible = false;
          }
        })
      }
    },
    close() {
      this.$refs.form.clearValidate();
      this.$emit('close');
      this.visible = false;
      this.model = {}
    },
    handleOk() {
      this.submit()
    },
    handleCancel() {
      this.close()
    },
    initDict() {
      roleList().then(res => {
        if (res.data.code === 200) {
          this.roleOptions = res.data.data
        } else {
          this.roleOptions = []
        }
      })
      single('sex').then(res => {
        if (res.data.code === 200) {
          this.sexOptions = res.data.data
        } else {
          this.sexOptions = []
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
